package com.pj.app.auraSimulator.parsers;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class KMLParser {
	private File file;
	
	private double[][] coords;
	
	private KMLParser(File file) {this.file = file;};
	
	public static KMLParser getInstance(File file) {
		return new KMLParser(file);
	}
	
	public void parse() throws ParserConfigurationException, SAXException, IOException {
		if(file != null) {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			
			
			
			NodeList coordinates = doc.getElementsByTagName("coordinates"); 
			Element firstnlElement = (Element) coordinates.item(0);
			NodeList fstNm = firstnlElement.getChildNodes();
			String c = ((Node) fstNm.item(0)).getNodeValue();
			String[] cArray = c.split(" ");
			
			coords = new double[cArray.length][3];
			
			for(int i = 0; i<cArray.length; i++) {
				String[] temp = cArray[i].split(",");
				coords[i][0] = Double.valueOf(temp[1]);
				coords[i][1] = Double.valueOf(temp[0]);
				coords[i][2] = Double.valueOf(temp[2]);
			}
		} 
	}
	
	public double[][] getCoords() {
		return coords;
	}
}
